import {
  addFriendApi,
  deleteFriendApi,
  getFriendApplyListApi,
  getFriendInfoApi,
  getFriendInfoByFriendIdApi,
  handleFriendApplyApi,
  updateFriendCircleAuthApi,
  updateFriendRemarkApi
} from '@renderer/api'
import { defineStore } from 'pinia'

const useFriend = defineStore('useFriend', {
  state: () => ({
    friendApplyList: [],
    friendApplyCount: 0
  }),
  actions: {
    // 获取好友信息
    setFriendInfo(id: number) {
      return new Promise((resolve) => {
        getFriendInfoApi(id).then((res) => {
          resolve(res.data)
        })
      })
    },
    // 修改好友备注
    editFriendNickname(e) {
      return new Promise((resolve, reject) => {
        updateFriendRemarkApi(e.nickname, e.id)
          .then((res) => {
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    // 添加好友
    addFriendAction(info) {
      return new Promise((resolve, reject) => {
        addFriendApi(info)
          .then((res) => {
            console.log(res)
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    // 删除好友
    deleteFriendAction(id) {
      return new Promise((resolve) => {
        deleteFriendApi(id).then((res) => {
          resolve(res)
        })
      })
    },
    // 获得好友申请列表
    getFriendApplyListAction() {
      return new Promise((resolve) => {
        getFriendApplyListApi().then((res) => {
          this.friendApplyList = res.data.rows
          this.friendApplyCount = res.data.count
          resolve(res.data)
        })
      })
    },
    // 处理好友申请
    handleFriendApplyAction(info) {
      return new Promise((resolve) => {
        handleFriendApplyApi(info).then((res) => {
          resolve(res)
        })
      })
    },
    // 修改圈子查看权限
    updateCirclePermissionAction(id, info) {
      return new Promise((resolve) => {
        updateFriendCircleAuthApi(id, info).then((res) => {
          resolve(res)
        })
      })
    },
    // 获取好友信息
    getFriendInfoAction(ids) {
      return Promise.all([
        getFriendInfoApi(ids.friend_id),
        getFriendInfoByFriendIdApi(ids)
      ])
    }
  }
})

export default useFriend
